var x = '1*2+3*4/2-4/2';
var nArray = []; // 值栈
var oArray = []; // 符栈

var OPERATOR = [['+',0],['-',0],['*',1],['/',1]]; // 设定操作符的优先级
var i = 0; // pointer to current op 当前op指针位置
var sNToken = x.charCodeAt(i) -48; // git the first number 拿到第一个值 // '0' 30h 48   '9' 39h 57
var sPToken = ''; // 运算符
var nPriority = 0; // priority 优先级
var iRet = ''; // 报错信息

if(sNToken < 0 || sNToken > 9) { // 如果不是1~9
    iRet = 'Error at position ' + i + ' : is not a number(' + x.charAt(i) + ')! Are you kidding me?';
}else { // 如果是1~9
    nArray.push(sNToken); // 把第一个数压栈
    i = 1; // 把指针移到第2个位置
    for(;i < x.length;) { // 指针遍历表达式
        sPToken = x.charAt(i); // get the operator 拿到运算符
        for(var j = 0; j < OPERATOR.length; j++) { // validate the operator 验证运算符是否有效
            if(OPERATOR[j][0] == sPToken) {
                break;
            }
        }
        if(j >= OPERATOR.length) { // 如果运算符不符合条件就跳出循环
            iRet = 'Error at position ' + i + ' : Syntax error: invalid operator \'' + sPToken + '\'';
            break;
        }
        nPriority = OPERATOR[j][1]; // get the priorty 获取优先级
        sNToken = x.charCodeAt(i+1) -48; // get the second number 获取第二个数
        if(sNToken < 0 || sNToken > 9) { // 如果下一个操作数不是1~9则跳出循环
            iRet = 'Error at position ' + (i + 1) + ' : is not a number(' + x.charAt(i + 1) + ')! Are you kidding me?';
            break;
        }

        // compare the priority of the operatore 比较操作数的优先级，如果是左侧的运算符优先级大或者相等就开始开始计算
        while(oArray.length != 0 && oArray[oArray.length-1][1] >= nPriority) {
            // 弹栈计算
            var n; // 计算结果
            var n2 = nArray.pop(); // 第一个操作数
            var n1 = nArray.pop(); // 第二个操作数
            var op = oArray.pop()[0]; // 操作符

            console.log('op = ' + op);
            switch(op) {
                case '+':
                    n = n1 + n2;
                    break;
                case '-':
                    n = n1 - n2;
                    break;
                case '*':
                    n = n1 * n2;
                    break;
                case '/':
                    n = n1 / n2;
                    break;
            }

            if(!isFinite(n) || isNaN(n)) {
                iRet = 'Error, the result is invalid!';
                break;
            }else { // 把计算好的结果压到值栈里面
                nArray.push(n);
            }
        }

        if(iRet) {
            break;
        }

        // 压栈
        nArray.push(sNToken);
        var opPair = [];
        opPair[0] = sPToken;
        opPair[1] = nPriority;

        oArray.push(opPair);
        i += 2;
    }

    while(oArray.length != 0) {
        // 弹栈计算
        var n;
        var n2 = nArray.pop();
        var n1 = nArray.pop();
        var op = oArray.pop()[0];

        switch(op) {
            case '+':
                n = n1 + n2;
                break;
            case '-':
                n = n1 - n2;
                break;
            case '*':
                n = n1 * n2;
                break;
            case '/':
                n = n1 / n2;
                break;
        }

        if(!isFinite(n) || isNaN(n)) {
            iRet = 'Error, the result is invalid!';
            break;
        }else {
            nArray.push(n);
        }
    }
}

// check
if(iRet) {
    console.log(iRet);
}else {
    console.log(nArray.pop());
}

